package com.his.modules.sysjob.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.modules.business.entity.CaSignDataLis;
import com.his.modules.business.service.CaSignDataLisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component("caSynBodyAndSjcLisJob")
@Slf4j
public class CaSynBodyAndSjcLisJob {
    @Autowired
    private CaSignDataLisService caSignDataLisService;
    /**
     * CA同步Body和时间戳
     * @return
     */
    public void caSynBodyAndSjcLisJob() {
        log.info("CA获取Body原文及时间戳定时任务开始执行=================");
        List<String> fileList = new ArrayList<>();
//        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\es-sign-1-8\\2024-03-26_order.txt");
//        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\es-sign-1-8\\2024-03-27_order.txt");
//        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\es-sign-1-8\\2024-03-28_order.txt");
//        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\es-sign-1-8\\2024-03-29_order.txt");
//        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\es-sign-1-8\\2024-03-30_order.txt");
//        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\es-sign-1-8\\2024-03-31_order.txt");
//        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\es-sign-1-8\\2024-04-01_order.txt");
        fileList.add("C:\\Users\\Administrator\\Desktop\\工作\\潍坊妇幼\\CA\\es-sign-1-8\\2024-04-02_order.txt");
        int lineCount, totalUpdate;
        for(String file : fileList) {
            lineCount   = 0;
            totalUpdate = 0;
            try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                String line;
                String data[];
                while ((line = br.readLine()) != null) {
                    lineCount ++;
                    log.info("读取文件{}第{}行的行内容：{}", file, lineCount, line);
                    data = line.split("#");
                    log.info("分割数组长度：{}", data.length);
                    if(data.length == 5) {
                        CaSignDataLis caSignData = caSignDataLisService.getById(data[0]);
                        if(!Objects.isNull(caSignData)) {
                            caSignData.setYwbody(data[2]);
                            caSignData.setQmsjc(data[3]);
                            boolean lb;
                            lb = caSignDataLisService.updateById(caSignData);
                            if (lb) totalUpdate ++;
                            log.info("CA_ID为{}的数据更新结果为{}", data[0], lb);
                        } else {
                            log.info("CA_ID为{}的数据未找到！", data[0]);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            log.info("文件{}共更新{}条！", file, totalUpdate);
        }
    }
}